#coding=utf-8
# filetree_filter_window.py - Modal Window to set file filter.
#
# Copyright 2011 Andrey Stepashov <Andrey.Stepashov@gmail.com>
# Copyright 2012 Maxim Petrov <maximpetrov@yahoo.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 3.

from modal_window import ModalWindow
from controls import CheckBox, Button, Label
import curses.ascii


class FiletreeFilterWindow(ModalWindow):
    """
    modal window class for setting filter options to current revision list
    """
    _PREFERRED_MIN_HEIGHT = 21
    _MIN_HEIGHT = 21
    _SHOULD_ENLARGE = False
    _message_visible = True
    _EXIT_BUTTON = u'Cancel'
    _title = _(u'File Filter')

    _OK = _(u'OK')
    _CANCEL = _(u'Cancel')

    __filter_option_max_width = 0


    def __init__(self, st_list):

        checkbox_captions = [u'[M] ' + _(u'modified'),
                             u'[A] ' + _(u'added'),
                             u'[R] ' + _(u'removed'),
                             u'[!] ' + _(u'deleted'),
                             u'[?] ' + _(u'unknown'),
                             u'[I] ' + _(u'ignored'),
                             u'[C] ' + _(u'clean')]

        caption_len = max(((len(c)) for c in checkbox_captions)) + 5

        for i, c in enumerate(checkbox_captions):
            checkbox_captions[i] = c + u' ' * (caption_len - len(c))

        self.__modified_checkbox = CheckBox(None, lambda w, h: (2, 4, None, None),
                                            checkbox_captions[0])
        self.__added_checkbox = CheckBox(None, lambda w, h: (2, 6, None, None),
                                            checkbox_captions[1])
        self.__removed_checkbox = CheckBox(None, lambda w, h: (2, 8, None, None),
                                            checkbox_captions[2])
        self.__deleted_checkbox = CheckBox(None, lambda w, h: (2, 10, None, None),
                                            checkbox_captions[3])
        self.__unknown_checkbox = CheckBox(None, lambda w, h: (2, 12, None, None),
                                            checkbox_captions[4])
        self.__ignored_checkbox = CheckBox(None, lambda w, h: (2, 14, None, None),
                                            checkbox_captions[5])
        self.__clean_checkbox = CheckBox(None, lambda w, h: (2, 16, None, None),
                                            checkbox_captions[6])
        self.__checkboxes = {u'M': self.__modified_checkbox,
                             u'A': self.__added_checkbox,
                             u'R': self.__removed_checkbox,
                             u'!': self.__deleted_checkbox,
                             u'?': self.__unknown_checkbox,
                             u'I': self.__ignored_checkbox,
                             u'C': self.__clean_checkbox}
        for c in st_list:
            self.__checkboxes[c].checked = True
        self.__show_label = Label(None,
            lambda w, h: (2, 2, None, None), _(u'Show') + u':')
        self.__ok_button = Button(None, lambda w, h:\
        (w / 4 - len(self._OK) / 2, h - 3, None, None),
            self._OK)
        self.__cancel_button = Button(None, lambda w, h:\
        (w * 3 / 4 - len(self._CANCEL) / 2 - 1, h - 3, None, None),
            self._CANCEL)
        self._controls = [(self.__modified_checkbox, True),
                          (self.__added_checkbox, True),
                          (self.__removed_checkbox, True),
                          (self.__deleted_checkbox, True),
                          (self.__unknown_checkbox, True),
                          (self.__ignored_checkbox, True),
                          (self.__clean_checkbox, True),
                          (self.__show_label, False),
                          (self.__ok_button, True),
                          (self.__cancel_button, True)]

        self._PREFERRED_MIN_WIDTH = caption_len + 8
        self._MIN_WIDTH = caption_len + 8



    def handle_keypress(self, ch, uni):
        """
        function is handling the pressed keys

        @param:
            ch - code of pressed key
            uni - ch in unicode

        if pressed key is closing the modal window,
        there are results returned to the caller as a dictionary.
        @return: {key, value}
            key = 'button' or name of filter option
            value = name of pressed button or set filter in textfield
        """

        result = ModalWindow.handle_keypress(self, ch, uni)
        if result is not None:
            return result

        # Key Tab: switch to next selected element
        if ch == ord('\t'):
            self._next_control()

        # Key Enter: exiting the modal window with result-dictionaries as return
        elif ch == ord('\n'):
            if self._selected == self.__cancel_button:
                return {u'button': u'Cancel'}
            else:
                return {u'button': u'OK',
                        u'st_list': [c[0] for c in self.__checkboxes.iteritems()
                                     if c[1].checked]}

        # Key Up: switch to previous checkbox
        elif ch == curses.KEY_UP and self._selected not in (self.__modified_checkbox,
                                                            self.__ok_button,
                                                            self.__cancel_button):
            self._previous_control()
        # Key Down: switch to down checkbox
        elif ch == curses.KEY_DOWN and self._selected not in (self.__clean_checkbox,
                                                              self.__ok_button,
                                                              self.__cancel_button):
            self._next_control()
        else:
            self._selected.handle_keypress(ch, uni)
